# -*- coding: utf-8 -*-
import json
import pickle

from script_utils import oid2nid
from script_utils import models
from app import redis_keys as rk


def migrate_constant():
    # key: locality_id
    # value: product_id
    LOCALITY_APPEND_PRODUCTS = {
        1: [73, 72],                    # 泰国
        2: [258],                       # 瑞士
        3: [258, 213, 166],             # 日本
        4: [258, 184, 335],             # 美国
        5: [258],                       # 德国
        6: [258],                       # 法国
        7: [258],                       # 意大利
    }

    _obj = {}
    for locality_id, product_ids in LOCALITY_APPEND_PRODUCTS.items():
        locality_id = str(oid2nid['vacation_locality'][locality_id])
        product_ids = filter(None, [str(oid2nid['vacation_product'].get(int(x), '')) for x in product_ids])
        _obj[locality_id] = product_ids

    models.KV.set(rk.LOCALITY_APPEND_PRODUCTS, _obj)

    # key: destination_id
    # value: product_id
    DESTINATION_APPEND_PRODUCTS = {
        64: [219, 176, 263],            # 东京
        71: [267, 221],                 # 大阪
        76: [223, 268],                 # 冲绳
        102: [186, 260, 264],           # 纽约
        109: [253, 389],                # 洛杉矶
        13: [82, 83],                   # 拜县
        2: [12, 13],                    # 普吉
        11: [89, 90],                   # 皮皮岛
        109: [382],                     # 旧金山
        103: [394],                     # 拉斯维加斯
        117: [331],                     # 西雅图
        123: [333],                     # 波士顿
        116: [380],                     # 圣地亚哥
        165: [332],                     # 芝加哥
    }

    _obj = {}
    for destination_id, product_ids in DESTINATION_APPEND_PRODUCTS.items():
        destination_id = str(oid2nid['vacation_destination'][destination_id])
        product_ids = filter(None, [str(oid2nid['vacation_product'].get(int(x), '')) for x in product_ids])
        _obj[destination_id] = product_ids

    models.KV.set(rk.DESTINATION_APPEND_PRODUCTS, _obj)


def migrate_redis_kv():
    '''redis_kv.json是用redis-rdb-tools从redis导出来的.
    导出命令: rdb --command json --db 0 --key "vacation*" vacationdev.rdb > redis_kv.json
    '''
    with open('redis_kv.json') as f:
        data = json.loads(f.read())[0]
        for k, v in data.items():
            try:
                v = pickle.loads(v)
            except:
                pass
            # print k, v
            # FIXME 之前是酒店产品跟旅行产品放一起，酒店产品是objectid类型，旅行产品是int类型，这样来区分，现在都是objectid了，没法区分，所以需要加多个kv key
            if k == 'vacation_feature_products':
                v = filter(None, [str(oid2nid['vacation_product'].get(int(x), '')) for x in v if x.isdigit()])
                # print 'v', v
            '''
            vacation_feature_products [u'169', u'717', u'561b93848e50216db03c3482', u'213', u'719', u'178', u'561b55be8e50215e5c419902', u'186', u'594', u'561b2d648e50215e5c41982c', u'556', u'546', u'604', u'174', u'184', u'215', u'210', u'251', u'222', u'249', u'211', u'225', u'19', u'88', u'45', u'2', u'26', u'722', u'209', u'205', u'176', u'186', u'358', u'360', u'354', u'170', u'304', u'692', u'28']
            '''
            models.KV.set(k, v)

if __name__ == '__main__':
    # migrate_constant()
    migrate_redis_kv()
